    /********************************************************/
    /*                                                      */
    /*                                                      */
    /*         Copyright (C), 1993  SEIKO EPSON Corp.       */
    /*         ALL RIGHTS RESERVED                          */
    /*                                                      */
    /*                                                      */
    /*  file name : fgets.c                                 */
    /*                                                      */
    /*  Revision history                                    */
    /*      93/12/17    first release       S.Akaiwa        */
    /*      97/3/17	modified	    T.Katahira	    */
    /*			change function declaration	    */
    /*					to the ANSI style   */
    /*                                                      */
    /********************************************************/

#ifdef  UNIX
#include    "stdio.h"
#else
#include    <stdio.h>
#endif

char    *
fgets(char * cBufP, int iBufSiz, FILE * fiStrmP)

    /********************************************************/
    /*                                                      */
    /*  The fgets function reads characters from the input  */
    /*  stream pointed to by fiStrmP.                       */
    /*  The data is read until number of characters         */
    /*  specified by iBufSiz - 1, until EOF is encountered  */
    /*  or '\n' character is read.                          */
    /*  The '\n' character is read, and the '\0' character  */
    /*  is written after the last character.                */
    /*  If stdin is not specified by fiStrmP, the fgets     */
    /*  function returns NULL pointer.                      */
    /*  NULL pointer cannot be specified for storage        */
    /*  pointer.                                            */
    /*                                                      */
    /*      RETURN  Except NULL ... Pointer to string       */
    /*              NULL        ... Error                   */
    /*                                                      */
    /*  <ANSI>                                              */
    /*      The fgets function reads a line from the user   */
    /*      specified stream.                               */
    /*                                                      */
    /*      RETURN  Except NULL ... Pointer to string       */
    /*              NULL        ... Error                   */
    /*                                                      */
    /********************************************************/

    /* char    *cBufP  ;     pointer to data storage buffer */
    /* int     iBufSiz ;       maximum number of characters */
    /* FILE    *fiStrmP;           pointer to target stream */
    {

     int    iOfst;
     int    iDat;

     if(fiStrmP != stdin){           /* stream except stdin */
        return  (char *)NULL;             /* illegal stream */
     }
     
     if(iBufSiz <= 1){
        if(iBufSiz == 1){                  /* buffer size 1 */
           cBufP[0] = '\0';               /* set terminator */
        }
        return   (char *)cBufP;   /* return storage pointer */ 
     }

     iOfst = 0;                             /* clear offset */

     while((iDat = getc(fiStrmP)) != '\n'){/* read until \n */
        if((iDat == EOF) || (iOfst >= iBufSiz - 1)){
            break;              /* found EOF or buffer full */
        } 
        else {
            cBufP[iOfst] = (char)iDat;          /* set data */
            iOfst++;
        }
     }
     
     if((iDat == '\n') && (iOfst < iBufSiz - 1)){
        cBufP[iOfst] = iDat;                    /* set '\n' */
        iOfst++;
     }
     
     cBufP[iOfst] = '\0';                 /* set terminator */
     
     if(iOfst == 0){
        cBufP[iOfst] = '\0';             
        return  (char *)NULL;        /* data does not exist */
     }
    
     return (char *)cBufP;        /* return storage pointer */
    }


